/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pl.tom.social.dal.entity;

import java.io.Serializable;
import java.util.Collection;
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

/**
 *
 * @author Tom
 */
@Entity
@Table(name = "Site")
@NamedQueries({@NamedQuery(name = "Site.findAll", query = "SELECT s FROM Site s ORDER BY s.url"),
	@NamedQuery(name = "Site.countAll", query = "SELECT COUNT(s.id) FROM Site s"),
	@NamedQuery(name = "Site.findAllUrls", query = "SELECT s.url FROM Site s ORDER BY s.url"),
	@NamedQuery(name = "Site.findAllByUpdate", query = "SELECT s FROM Site s ORDER BY s.updated"),
	@NamedQuery(name = "Site.findByUrl", query = "SELECT s FROM Site s WHERE s.url = :url"),
	@NamedQuery(name = "Site.findNewest", query = "SELECT s FROM Site s ORDER BY s.updated DESC")})
public class Site implements Serializable {

	private static final long serialVersionUID = 1L;
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Basic(optional = false)
	@Column(name = "id")
	private Integer id;
	@Basic(optional = false)
	@Column(name = "url")
	private String url;
	@Basic(optional = false)
	@Column(name = "added")
	@Temporal(TemporalType.TIMESTAMP)
	private Date added;
	@Column(name = "updated")
	@Temporal(TemporalType.TIMESTAMP)
	private Date updated;
	@ManyToMany(mappedBy = "siteCollection")
	private Collection<Owner> ownerCollection;
//	@OneToMany(cascade = CascadeType.ALL, mappedBy = "siteId")
//	private Collection<SiteLink> siteLinkCollection;
	@OneToOne(cascade = CascadeType.ALL, mappedBy = "site")
	private SiteDetails siteDetails;
//	@OneToMany(cascade = CascadeType.ALL, mappedBy = "siteId", fetch=FetchType.LAZY)
//	private Collection<SiteEntry> siteEntryCollection;

	public Site() {
	}

	/***
	 * Creates site class. SETS ADDED DATE TO NOW.
	 * @param url Url address of a site.
	 */
	public Site(String url) {
		this(url, new Date());
	}

	public Site(String url, Date added) {
		this.url = url.toLowerCase();
		this.added = added;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url.toLowerCase();
	}

	public Date getAdded() {
		return added;
	}

	public void setAdded(Date added) {
		this.added = added;
	}

	public Date getUpdated() {
		return updated;
	}

	public void setUpdated(Date updated) {
		this.updated = updated;
	}

	public Collection<Owner> getOwnerCollection() {
		return ownerCollection;
	}

	public void setOwnerCollection(Collection<Owner> ownerCollection) {
		this.ownerCollection = ownerCollection;
	}

	public SiteDetails getSiteDetails() {
		return siteDetails;
	}

	public void setSiteDetails(SiteDetails siteDetails) {
		this.siteDetails = siteDetails;
	}

	@Override
	public int hashCode() {
		int hash = 0;
		hash += (id != null ? id.hashCode() : 0);
		return hash;
	}

	@Override
	public boolean equals(Object object) {
		if(!(object instanceof Site)) {
			return false;
		}
		Site other = (Site) object;
		if((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
			return false;
		}
		return true;
	}

	@Override
	public String toString() {
		return this.url;
	}
}
